<?php
// 获取商品列表后端
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");

if (empty($_GET['ways']) || $_GET['ways'] == 'all') {
    $sql = "SELECT * FROM tb_goods_info ORDER BY g_sell_num DESC";
} elseif ($_GET['ways'] == '蔬菜') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='蔬菜' ORDER BY g_sell_num DESC";
} elseif ($_GET['ways'] == '水果') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='水果' ORDER BY g_sell_num DESC";
} elseif ($_GET['ways'] == '生鲜') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='生鲜' ORDER BY g_sell_num DESC";
} elseif ($_GET['ways'] == '粮油') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='粮油' ORDER BY g_sell_num DESC";
} elseif ($_GET['ways'] == 'name') {
    $name  = $_GET['name'];
    $sql = "SELECT * FROM tb_goods_info WHERE g_name LIKE '%" . $name . "%' ORDER BY g_sell_num DESC";
}

$result = mysqli_query($conn, $sql);

if($result){
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $s = intval($row['g_num']) - intval($row['g_sell_num']);
            $sta = '';
            if($row['g_is_sell'] == 1){
                $sta = "<td style='color:lightgreen'>商品在售</td>";
            }else{
                $sta = '<td style="color:tomato">商品已下架</td>';
            }
            echo"
                <tr>
                    <th scope='row'>".$row['g_id']."</th>
                    <td>".$row['g_name']."</td>
                    <td>".$row['g_num']."</td>
                    <td>".$row['g_sell_num']."</td>
                    <td>".$s."</td>
                    <td>".$row['g_specs']."</td>
                    <td>".$row['g_price']."</td>
                    <td>".$row['g_adddate']."</td>".$sta."
                </tr>
            ";
        }
    }else{
        echo"<tr><td colspan='8'>暂无信息</td></tr>";
    }
}else{
    echo"<tr><td colspan='8'>查询失败</td></tr>";
}